package com.stedu.controller;

import com.stedu.bean.RespBean;
import com.stedu.service.AdminService;
import com.stedu.service.LikeModelsService;
import com.stedu.service.ModelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/likemodels")
public class LikeModelsController {
    @Autowired
    private LikeModelsService likeModelsService;
    @Autowired
    private ModelsService modelsService;
    @Autowired
    private AdminService adminService;
    @GetMapping("/allAdmin/{mid}")
    public RespBean allAdmin(@PathVariable("mid")Integer mid){
        //{{key：用户的id label:用户真实姓名}}
        List<Map<String, Object>> adminMapList = adminService.selectPage(null)
                .stream()
                .filter(e -> e.getUser().equals("用户"))
                .map(e -> {
                    Map<String, Object> adminMap = new HashMap<>();
                    adminMap.put("key", e.getId());
                    adminMap.put("label", e.getName() + "-" + e.getGender());
                    return adminMap;
                }).collect(Collectors.toList());
            //获取已经分配的员工的mid
        List<Integer> ids=likeModelsService.selectIdByMid(mid);
        Map<String, Object> map = new HashMap<>();
        map.put("allAdmin", adminMapList);
        map.put("selectedids", ids);
        return  RespBean.ok("",map);
    }
    @PostMapping("midAndid")
  public RespBean insertmidAndid(@RequestBody Map<String, Object> map){
        System.out.println(map);

        //likeModelsService.insertMidAndId(mid, ids);
        Integer mid = (Integer) map.get("mid");
       List<Integer> idslist =(List<Integer>)map.get("ids");
       Integer[] ids = new Integer[idslist.size()];
       for (int i=0;i< idslist.size();i++)
       {int id = idslist.get(i);
           ids[i]=id;
       }
            likeModelsService.insertMidAndId(mid,ids);
        return  RespBean.ok("分配用户喜好成功");
    }

}
